/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.heathrow.servicesImpl;

import cz.muni.fi.heathrow.dao.StewardDAO;
import cz.muni.fi.heathrow.dto.DTOAssembler;
import cz.muni.fi.heathrow.entities.Steward;
import cz.muni.fi.heathrowapi.StewardService;
import cz.muni.fi.heathrowapi.dto.StewardDTO;
import cz.muni.fi.heathrowapi.HeathrowRoles;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Tomas
 */

public class StewardServiceImpl implements StewardService {
    
    
    private StewardDAO stewardDao;
    
    public void setStewardDao(StewardDAO steward) {
        this.stewardDao = steward;
    }
    
    public StewardDAO getStewardDao() {
        return stewardDao;
    }
    
   @Transactional(readOnly=true)
    @Override
    public StewardDTO get(Long id) {
        Steward steward = stewardDao.get(id);
        StewardDTO stewardDTO;
        
        if(steward == null) return null;
        
        stewardDTO = DTOAssembler.CreateStewardDTO(steward);
        
        return stewardDTO;
    }

   @Transactional
    @Override
    @PreAuthorize("hasRole('" + HeathrowRoles.ADMIN + "')")
    public void create(StewardDTO steward) {
       if(steward == null) throw new IllegalArgumentException("Steward is null");
       
       Steward stewardLoc = new Steward();
       DTOAssembler.UpdateStewardDomainObject(steward, stewardLoc);
       stewardDao.create(stewardLoc);
        DTOAssembler.UpdateStewardDTO(steward, stewardLoc);
    }

   @Transactional
    @Override
    @PreAuthorize("hasRole('" + HeathrowRoles.ADMIN + "')")
    public void update(StewardDTO steward) {
        if(steward == null) throw new IllegalArgumentException("Steward is null");
        
        Steward stewardLoc = stewardDao.get(steward.getId());
        DTOAssembler.UpdateStewardDomainObject(steward, stewardLoc);
        stewardDao.update(stewardLoc);
    }

   @Transactional
    @Override
    @PreAuthorize("hasRole('" + HeathrowRoles.ADMIN + "')")
    public void remove(StewardDTO steward) {
        Steward stewardLoc = stewardDao.get(steward.getId());
        stewardDao.remove(stewardLoc);
    }
   
   @Transactional
    @Override
    @PreAuthorize("hasRole('" + HeathrowRoles.USER + "')")
    public List<StewardDTO> findAll() {
       return DTOAssembler.CreateStewardDTOList(stewardDao.findAll());
    }

   @Transactional
    @Override
    @PreAuthorize("hasRole('" + HeathrowRoles.USER + "')")
    public StewardDTO findByPersonalId(Long personalId) {
        Steward stewardLoc = stewardDao.findByPersonalId(personalId);
        return DTOAssembler.CreateStewardDTO(stewardLoc);
    }

    @Transactional
    @Override
    @PreAuthorize("hasRole('" + HeathrowRoles.USER + "')")
    public List<StewardDTO> findByAnyName(String anyName, int limit) {
        return DTOAssembler.CreateStewardDTOList(stewardDao.findByAnyName(anyName, limit));
    }
    

}
